#! /usr/bin/env python
# -*- coding: utf-8 -*-

from operator import le
from re import S
from playwright.sync_api import Page
from ui_automation_test.common.base_page import BasePage
from ui_automation_test.locators.dualPrevention.riskCheckTaskManagement.planConfig.warrantyLiabilityPlanConfig.loc_warranty_liability_plan_config import LocWarrantyLiabilityPlanConfig
from ui_automation_test.locators.dualPrevention.riskCheckTaskManagement.planConfig.warrantyLiabilityPlanConfig.loc_warranty_liability_plan_search import LocSearchWarrantyLiability
from ui_automation_test.locators.dualPrevention.riskCheckTaskManagement.planConfig.warrantyLiabilityPlanConfig.loc_warranty_liability_plan_view import LocViewWarrantyLiability
from ui_automation_test.common.logger import logger


class WarrantyLiabilityPlanConfig(BasePage):

    def __init__(self, page: Page):
        super().__init__(page)
        self.switch_menu('双重预防机制')
        self.switch_menu('隐患排查任务管理')
        self.switch_menu('计划配置')
        self.switch_menu('包保责任计划配置')       
    

    def config_warranty_liability_plan(self, investigation_level, initial_inspection_date, initial_inspection_time, work_calendar, investigation_method, dept, person, allow_recheck_time, allow_recheck_time_unit):
        """
        配置包保责任排查计划
        :param investigation_level: 排查层级
        :param initial_inspection_date: 初始排查日期
        :param initial_inspection_time: 初始排查时间
        :param work_calendar: 工作日历
        :param investigation_method: 排查方式
        :param allocation_method: 分配方式
        :param dept: 部门
        :param person: 人员
        :param allow_recheck_time: 允许补检时间
        :param allow_recheck_time_unit: 允许补检时间单位
        """
        
        # 筛选出配置状态为“未配置”的计划
        table = self.page.locator('.el-table__body')
        row = table.locator('tr').filter(has_text='未配置').nth(0)
        button = row.locator('td').nth(15)
        logger.info('点击配置按钮')
        button.get_by_text('配置').click()
        self.page.wait_for_timeout(500)

        # 计划配置
        logger.info('选择排查层级')
        self.select(LocWarrantyLiabilityPlanConfig.loc_investigation_level, investigation_level)
        self.page.wait_for_timeout(500)
        # 设置初始排查时间
        self.fill(LocWarrantyLiabilityPlanConfig.loc_initial_inspection_date, initial_inspection_date)
        self.page.wait_for_timeout(500)
        self.fill(LocWarrantyLiabilityPlanConfig.loc_initial_inspection_time, initial_inspection_time)
        self.page.wait_for_timeout(500)
        logger.info('点击初始排查时间框确定按钮')
        self.page.locator(".el-time-panel__btn.confirm").get_by_text('确定').click()
        self.page.wait_for_timeout(500)
        logger.info('选择工作日历')
        if work_calendar == "每天":
            self.check_complex(LocWarrantyLiabilityPlanConfig.loc_work_calendar, '每天')
        elif work_calendar == "工作日":
            self.check_complex(LocWarrantyLiabilityPlanConfig.loc_work_calendar, '工作日')
        elif work_calendar == "非工作日":
            self.check_complex(LocWarrantyLiabilityPlanConfig.loc_work_calendar, '非工作日')

        logger.info('选择排查方式')
        if investigation_method == "智能巡检系统":  
            self.check_complex(LocWarrantyLiabilityPlanConfig.loc_investigation_method, '智能巡检系统')
        elif investigation_method == "隐患排查系统":
            self.check_complex(LocWarrantyLiabilityPlanConfig.loc_investigation_method, '隐患排查系统')
        
        # 选择排查人员
        logger.info('点击选择人员按钮')
        self.click(LocWarrantyLiabilityPlanConfig.loc_select_person_button)
        logger.info('点击部门')
        self.page.get_by_text(dept).first.click()
        self.page.wait_for_timeout(500)
        logger.info('勾选人员')
        self.page.get_by_text(person).first.click()
        self.page.wait_for_timeout(500)
        logger.info('点击右移按钮')
        self.page.get_by_role('dialog', name='选择人员').get_by_role('button', name='', exact=True).nth(1).click()
        self.page.wait_for_timeout(1000)
        logger.info('点击确定按钮')
        self.click(LocWarrantyLiabilityPlanConfig.loc_confirm_button)
        self.page.wait_for_timeout(1000)
        if investigation_method == "智能巡检系统":
            logger.info('输入允许补检时间')
            self.fill_number(LocWarrantyLiabilityPlanConfig.loc_allow_recheck_time, allow_recheck_time)
            logger.info('选择允许补检时间单位')
            self.select(LocWarrantyLiabilityPlanConfig.loc_allow_recheck_time_unit, allow_recheck_time_unit, exact=True)
            logger.info('点击巡检点按钮')
            self.click(LocWarrantyLiabilityPlanConfig.loc_inspection_point_button)
            logger.info('勾选巡检点数据复选框')
            self.select_row_checkbox('巡检点1')
            logger.info('点击确认添加按钮')
            self.click(LocWarrantyLiabilityPlanConfig.loc_confirm_add_inspection_point)
            self.page.wait_for_timeout(1000)
            logger.info('点击管理巡检标准按钮')
            self.click(LocWarrantyLiabilityPlanConfig.loc_manage_inspection_standard_button)
            logger.info('勾选巡检标准数据复选框')
            self.select_row_checkbox('自动化检查项')
            logger.info('点击确认添加按钮')
            self.click(LocWarrantyLiabilityPlanConfig.loc_confirm_add_inspection_standard)
            self.page.wait_for_timeout(1000)
            logger.info('点击确定按钮')
            self.click(LocWarrantyLiabilityPlanConfig.loc_submit)
        elif investigation_method == "隐患排查系统":
            logger.info('点击确定按钮')
            self.click(LocWarrantyLiabilityPlanConfig.loc_submit)

    def search_warranty_liability_plan(self, company, object_name, unit_name, plan_status):
        """
        查询包保责任排查计划
        :param company: 所属企业
        :param object_name: 所属风险分析对象
        :param unit_name: 所属风险分析单元        
        :param plan_status: 计划状态
        """
        logger.info('开始包保责任排查计划查询')
        if company:
            logger.info(f'选择所属企业: {company}')
            self.select(LocSearchWarrantyLiability.loc_company_select, company)
        if object_name:
            logger.info(f'选择所属风险分析对象: {object_name}')
            self.select(LocSearchWarrantyLiability.loc_object_select, object_name)
        if unit_name:
            logger.info(f'选择单元名称: {unit_name}')
            self.select(LocSearchWarrantyLiability.loc_unit_name_select, unit_name)
        if plan_status:
            logger.info(f'选择计划状态: {plan_status}')
            self.select(LocSearchWarrantyLiability.loc_plan_status_select, plan_status)
        logger.info('点击查询按钮')
        self.click(LocSearchWarrantyLiability.loc_search_button)

    def view_warranty_liability_plan(self, company, object_name, unit_name, plan_status):
        """
        查看包保责任排查计划详情
        :param company: 所属企业
        :param object_name: 所属风险分析对象
        :param unit_name: 所属风险分析单元
        :param investigation_level: 排查层级
        :param plan_status: 计划状态
        """
        logger.info('开始包保责任排查计划查看')
        self.search_warranty_liability_plan(company, object_name, unit_name, plan_status)
        self.page.wait_for_timeout(1000)
        logger.info('点击查看按钮')
        self.click(LocViewWarrantyLiability.loc_view_button)
        logger.info('查看包保责任排查计划详情')
        self.page.wait_for_timeout(1000)